package com.sskj.xyft.market;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.bumptech.glide.request.RequestOptions;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;
import com.sskj.base.adapter.BaseAdapter;
import com.sskj.base.adapter.ViewHolder;
import com.sskj.base.utils.ClickUtil;
import com.sskj.base.view.DividerLineItemDecoration;
import com.sskj.base.view.ToolBarLayout;
import com.sskj.xyft.MVPActivity;
import com.sskj.xyft.R;
import com.sskj.xyft.WebActivity;
import com.sskj.xyft.message.data.MessageData;

import butterknife.BindView;

public class NoticeListActivity extends MVPActivity<NoticeListPresenter> {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R.id.toolbar)
    ToolBarLayout toolbar;

    BaseAdapter<MessageData.Message> adapter;
    int page = 1;
    int pageSize = 10;

    @Override
    public int getLayoutId() {
        return R.layout.activity_notice_list;
    }

    @Override
    public NoticeListPresenter getPresenter() {
        return new NoticeListPresenter();
    }

    @Override
    public void initView() {
        initRefreshLayout(smartRefreshLayout);
        toolbar.setLeftButtonOnClickListener(v -> finish());
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth(2)
                .setFirstDraw(false)
                .setDividerColor(color(R.color.lineColor))
        );
        adapter = new BaseAdapter<MessageData.Message>(R.layout.item_message, null, recyclerView, true) {
            @Override
            public void bind(ViewHolder holder, MessageData.Message item) {
                holder.setText(R.id.message_title, item.getTitle())
                        .setText(R.id.message_time, item.getCreate_time());

                RequestOptions requestOptions = new RequestOptions()
                        .error(R.mipmap.icon_default)
                        .placeholder(R.mipmap.icon_default);
//                Glide.with(NoticeListActivity.this).load(BuildConfig.BASE_URL + item.getImgCover()).apply(requestOptions).into((ImageView) holder.getView(R.id.message_img));
                ClickUtil.click(holder.getView(R.id.itemLayout), view -> {
                    WebActivity.start(NoticeListActivity.this, item.getId() + "", 1);
                });
            }
        };
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshLayout) {
                page++;
                mPresenter.getNoticeList(account, page, pageSize, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshLayout) {
                page = 1;
                mPresenter.getNoticeList(account, page, pageSize, false);
            }
        });
    }

    @Override
    public void initData() {
        mPresenter.getNoticeList(account, page, pageSize, true);
    }


    public void setData(MessageData data) {
        if (data != null) {
            if (page == 1) {
                adapter.setNewData(data.getData());
            } else {
                adapter.addData(data.getData());
            }
        } else {
            adapter.setNewData(null);
        }
    }

    public void finishRefresh() {
        smartRefreshLayout.finishLoadMore();
        smartRefreshLayout.finishRefresh();
    }


    public static void start(Context context) {
        Intent intent = new Intent(context, NoticeListActivity.class);
        context.startActivity(intent);
    }
}
